//
//  YTCarsManagerViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/29.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTCarsManagerViewController.h"
#import "YTCarsManagerViewCell.h"
#import "YTDriverCarServices.h"
#import "YTDriverCarModel.h"
#import "YTCarInfoViewController.h"
#import <YYWebImage/YYWebImage.h>
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface YTCarsManagerViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
/** 数据源*/
@property (strong, nonatomic) NSMutableArray<YTDriverCarModel *> *dataArray;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *addBtn;
@end

@implementation YTCarsManagerViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadCarData];
}

- (void)initialize {
    self.tableView.tableFooterView = [UIView new];
    _dataArray = [NSMutableArray array];
    self.addBtn.enabled = !([YTUtilityManager manager].userModel.userRoleNew == 1);
    [self.tableView registerNib:[UINib nibWithNibName:@"YTCarsManagerViewCell" bundle:nil] forCellReuseIdentifier:@"YTCarsManagerViewCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTCarsManagerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTCarsManagerViewCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    @weakify(self);
    cell.deleteAction = ^(NSString *car_id) {
        @strongify(self);
        [self deleteCarWithID:self.dataArray[indexPath.row].carId];
    };
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSegueWithIdentifier:@"YTCarDetailInfo" sender:_dataArray[indexPath.row].carId];
}

#pragma mark - DZNEmptyDataSetSource

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"您目前还没有任何车辆哦~";
    if ([YTUtilityManager manager].userModel.userRoleNew == 1) {
        text = @"您尚未完成司导认证无法添加车辆，\n请先完成司导认证哦~";
    }
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f],
                                 NSForegroundColorAttributeName: kGrayColor};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *image = @"empty_car";
    return [UIImage imageNamed:image];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}

- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    if ([YTUtilityManager manager].userModel.userRoleNew == 1) {
        return [UIImage imageNamed:@"profile_auth_now"];
    } else {
        return [UIImage imageNamed:@"profile_addcar"];
    }
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if ([YTUtilityManager manager].userModel.userRoleNew == 1) {
        [self performSegueWithIdentifier:@"YTDriverAuth" sender:nil]; // 立即认证
    } else {
        [self performSegueWithIdentifier:@"YTAddCar" sender:nil]; // 添加车辆
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTCarDetailInfo"]) {
        YTCarInfoViewController *carInfo = segue.destinationViewController;
        carInfo.car_id = sender;
    }
}

#pragma mark - 删除车辆

- (void)deleteCarWithID:(NSString *)car_id {
    [UIAlertView bk_showAlertViewWithTitle:@"友情提示"
                                   message:@"您确定要删除该车辆吗?"
                         cancelButtonTitle:@"取消"
                         otherButtonTitles:@[@"确定"]
                                   handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
       @weakify(self);
       if (buttonIndex == 1) {
           NSMutableDictionary *dict = [NSMutableDictionary dictionary];
           dict[@"carId"] = car_id;
           [[YTDriverCarServices deleteCarWith:dict] subscribeNext:^(NSDictionary *response) {
               @strongify(self);
               if ([response[@"resultCode"] integerValue] == 1000) {
                   [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                   [self loadCarData];
               }
           }];
       }
    }];
}

#pragma mark - 加载数据

- (void)loadCarData {
    [SVProgressHUD show];
    [[YTDriverCarServices getCarListData:nil] subscribeNext:^(NSDictionary *response) {
        [SVProgressHUD dismiss];
        NSDictionary *dict = response[@"responseData"];
        _dataArray = [[NSArray yy_modelArrayWithClass:[YTDriverCarModel class] json:dict[@"carList"]] mutableCopy];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        [SVProgressHUD dismiss];
    }];
}

@end
